<?

include_once("./_common.php");

include_once("./admin.head.php");

//셀렉트 박스에 쓰일 이벤트제목(이벤트글 제목)과 wr_id를 코멘트를 뺀 원글만 가져옴.
//그전에 등록한 이벤트들 필요없으면 where절에서 id값으로 처리. 필요하면 쓰기에서 필드다채워야함

$sql1 = "SELECT		wr_subject, wr_id
		 FROM		g4_write_event
		 WHERE		wr_is_comment = '0'
		 ORDER BY	wr_id DESC";
$rst1 = sql_query($sql1);
while($row1 = mysql_fetch_assoc($rst1)) {
	$subj[] = $row1;
}
$memberList = array();
for($i=0; $i<count($subj); $i++){
	$memberList[$i] = '"'.$subj[i].'"';
}
$sql = "select * from g4_member where mb_level = 2 order by mb_id desc";
$rst = sql_query($sql);
$issue = array();
while($row = mysql_fetch_assoc($rst)) {
	$issue[] = $row;
}

if($cu_no){
	$sql = "select * from coupon_win where cu_no = $cu_no order by id desc";
	$rst = sql_query($sql);
	$list = array();
	while($row = mysql_fetch_assoc($rst)) {
		$list[] = $row;
	}
	$rst_list = array();
	for($i=0; $i<count($list); $i++){
		$tmp = $list[$i]['mb_id']."\t".$list[$i]['mb_name']."\t".$list[$i]['mb_email']."\t".$list[$i]['mb_birth'];
		
		$rst_list[$i] = '"'.$tmp.'"';
	}
	
}


?>

<style>
div.input_wrap {width:700px;margin:0 auto; }
table.input_table {margin-top:20px;width:688px;border:1px solid #d9d9d9;}
table.input_table tr {height:36px; border-top:1px solid #d9d9d9;}
table.input_table th { #d9d9d9; width:120px}
div.input_btn {width:698px;text-align:center;margin-top:350px}
.input_div {width: 100%; height:50px; font-family:'맑은 고딕'}
</style>
<?=subtitle("쿠폰 발급")?>
<div class="input_wrap">

	<form  method="post" name="fsel" >
		<input type=hidden name=token value='<?=$token?>'>
		<input type=hidden name=bo_table value='<?=$bo_table?>'>
		<input type=hidden name=cu_no value='<?=$cu_no?>'>
		<input type="hidden" name="to_value">
		<input type="hidden" name="update">
		
		<div class="input_div">
			<th><b>이벤트 &nbsp;&nbsp;&nbsp;</b></th>
			<td><select name="subj" class="subj">
				<option value="">해당 이벤트 없음</option>
				<?php
				for($i = 0; $i < count($subj); $i += 1) { ?>
				<option value="<?php echo $subj[$i]['wr_id']?>" <?php echo $subj[$i]['wr_id'] == $lately_event_total ? "selected" : ""?>> <?php echo $subj[$i]['wr_subject']?> </option>
				<? } ?>
			</select></td>
			
		</div>
		<div style="height:30px;">
			<tr>
				<td><b>-회원 정보 리스트 &nbsp;&nbsp;&nbsp;&nbsp;</b></td>
				</tr>
			
		</div>
		<div class="input_list" style="float:left; font-family:'맑은 고딕'">
				
				<td><input type="radio" class="all" name="ck" id="ck_1" ><b>모두선택</b></b></input></td>
				<select id="id_sel_pims" name="input_sel_pims" style="display:block; height:300px; width:300px" multiple="multiple">
					<?php
					for($i=0; $i<count($issue); $i++){
						$content = "";
						if($issue[$i]['mb_name']){
							$content .= "[";
							$content .= $issue[$i]['mb_id']."\t".$issue[$i]['mb_name']."\t".$issue[$i]['mb_email']."\t".$issue[$i]['mb_birth'];
							$content .= "]";
						}
						?>
						<option value="<?php echo $issue[$i]['mb_id']."\t".$issue[$i]['mb_name']."\t".$issue[$i]['mb_email']."\t".$issue[$i]['mb_birth'] ?>"><?php echo $content?> </option>
						<?php 
					}
					?>
				</select>
				</tr>
				<script type="text/javascript">
					$(".all").click(function(){
						var obj = document.getElementById("id_sel_pims");
						var cnt=obj.options.length;
						for( var i=0; i<=cnt; i++){
							obj.options[i].selected = true; 
						} 
					});
				</script>
		</div>
		<div class="input_list" style="top:184px; float:left; overflow:hidden; left:330px; position:absolute; ">
			<dl class="f_clear" style="margin-bottom:8px">
				
				<dd class="btn_list" style="width:68px; margin-top:27px; text-align:center;">
					<button class="btn_small" type="button" style="width:auto" onClick="javascript:addrpopname.addToSelect('to')">
						<img alt="추가" src="./img_btn/plus_btn.gif"></img>
					</button>
					<button class="btn_small" type="button" style="width:auto" onClick="javascript:addrpopname.deleteFromSelect('to')">
						<img alt="삭제" src="./img_btn/minu_btn.gif"></img>
					</button>
				</dd>
				
			</dl>
			<script type="text/javascript">
				var addrpopname={
					pimsdata:<? echo '['.implode(',', $memberList).']'; ?>
					,addToSelect:function(t){
						var $ss=$('#id_sel_pims :selected');
						var c=[];
						var $ts=$('#id_sel_'+t);
						var ts=$ts.get(0);
						$.each(ts.options,function(i,v){c.push(v.value)});
						$.each($ss,function(i,v){
							if($.inArray(v.value,c)!==-1)return;
							$ts.append('<option value="'+decodeURI(v.value)+'">'+decodeURI(v.value).replace('<','&lt;').replace('>','&gt;')+'</option>');
						});
						// 주소록 추가 및 삭제 카운팅
						countSelect();
						
					}
					,deleteFromSelect:function(f){
						$('select[name='+f+'] :selected').remove();

						// 주소록 추가 및 삭제 카운팅
						countSelect();
					}
					,getSelectValues:function(from){
						var a=[];
						var s=$('#id_sel_'+from).get(0);
						$.each(s.options,function(i,o){
							if(o.value=='')return;a.push(o.value);
						});
						return a.join(',');
					}
				}

				var countSelect = function(){
					$('#cntRecv').text(' '+ $('#id_sel_to').children().size());
				}
				
				
			</script>
		</div>
		<div class="input_to" style="overflow:hidden; left:405px; position:absolute;  font-family:'맑은 고딕'">
			<dt>
				<label for="id_sel_to"><b>받는 사람</b></label>
				<strong class="num" id="cntRecv" style="color: red"> 0</strong>
			</dt>
			<dd style="float:left">
				<select id="id_sel_to" name="to" style="display:block; height:300px; width:300px" multiple="multiple">
				</select>
				<script type="text/javascript">
					$(document).ready(function(){
						addmemberList.loadTosData();

						if(  $('#id_sel_to').children().size() > 0 ){
								$(".update_btn").show();
								$(".save_btn").hide();
						}
					});
					
					var addmemberList={
						todata:<? echo '['.implode(',', $rst_list).']'; ?>
						,loadTosData:function(){
							var $p=$('#id_sel_to');
							$p.empty();
							$.each(this.todata,function(i,v){
								if(v=='')return;
								$p.append('<option value="'+decodeURI(v)+'">'+v.replace('<','&lt;').replace('>','&gt;')+'</option>');
							});
							countSelect();
							return this;
						}
					}
				</script>
			</dd>
			
		</div>
		<div class="input_btn">
			<img src="./img_btn/save_btn.gif" style="cursor:pointer; display:none" class="update_btn"/>
			<img src="./img_btn/save_btn.gif" style="cursor:pointer" class="save_btn"/>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<img src="./img_btn/list_btn.gif" class="list_btn" style="cursor:pointer" />
		</div>
		
	</form>

</div>
	
<script>

var setAndSubmit = function(a){
	var f=document.fsel;
	var to_arr=(this.getSelectValues("to"))?(this.getSelectValues("to")).split(',') : null ; // 배열 타입
	f.to_value.value = to_arr;
	f.action=a;
	f.submit();
}
// id_sel_to 에 있는 option 정보들 설정 
var	getSelectValues = function(from){
	var a=[];
	var s=$('#id_sel_'+from).get(0);
	$.each(s.options,function(i,o){
		if(o.value=='')return;a.push(o.value);
	});
	return a.join(',');
}

$(".update_btn").click(function() {
	setAndSubmit('./coupon_issue_update.php?w=u');
});

$(".save_btn").click(function() {

	setAndSubmit('./coupon_issue_update.php');
});

$(".list_btn").click(function() {
	location.href = "./coupon_list.php?bo_table=coupon";
});
$(".excel_btn").click(function() {
	var xc = $(".subj").val();
	location.replace("<?php echo $g4[bbs_path]?>/coupon_excel.php?idx=" + xc);
});
</script>


